<?php

/**
 * This is the model class for table "mobile".
 *
 * The followings are the available columns in table 'mobile':
 * @property string $id
 * @property string $name
 * @property string $model
 * @property string $ctime
 * @property string $mtime
 *
 * The followings are the available model relations:
 * @property MobileAdditional[] $mobileAdditionals
 * @property MobileDescription[] $mobileDescriptions
 * @property MobileFile[] $mobileFiles
 */
class Mobile extends CActiveRecord
{
    public $description;
    public $thumbs;
    public $configuration;
    public $noImage = '/images/noimage.png';
    public $files;
    public $moneys;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Mobile the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'mobile';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, model', 'required'),
			array('name, ctime, mtime', 'length', 'max'=>11),
			array('model', 'length', 'max'=>80),
                        array('money', 'numerical'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, money,name, model, ctime, mtime', 'safe', 'on'=>'search'),
		);
	}
        
        public function beforeSave() {
            if($this->isNewRecord)
                $this->ctime = $this->mtime = time();
            else
                $this->mtime = time();
            return parent::beforeSave();
        }

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'mobileAdditionals' => array(self::HAS_MANY, 'MobileAdditional', 'mobile_id'),
			'mobileDescriptions' => array(self::HAS_ONE, 'MobileDescription', 'mobile_id'),
			'mobileFiles' => array(self::HAS_MANY, 'MobileFile', 'post_id'),
                        'mobileBrands'=>array(self::BELONGS_TO,'MobileBrand','name'),
		);
	}
        
        public function afterFind() 
        {
            $files = array();
            $file = array();
            $image = '';
            $file = $this->mobileFiles;
            foreach($file as $key=>$n)
            {
                $files = unserialize($n->url);
            }
            $this->files = $files;
            $files = array_reverse($this->files);
            foreach ($files as $key=>$n)
            {
                $image = $n;
            }
            if(0 < strlen($image))
                $this->thumbs = Yii::app()->request->baseUrl.'/'.str_replace('albums', 'thumbs', $image);
            else
            {
                $this->thumbs = $this->noImage;
            }
            $this->moneys = $this->setMoney($this->mobileAdditionals);
            $this->name = $this->mobileBrands->name;
            $this->ctime = date('Y-m-d',  $this->ctime);
            $this->mtime = date('Y-m-d',  $this->mtime);
        }
        
        public function setMoney($params)
        {
            $resault = array();
            foreach ($params as $key=>$n)
            {
                $resault[] = $n['color'].' '.$n['version'].' ￥<font color=red>'.$n['money'].'</font>';
            }
            return implode(' | ',$resault);
        }


        /**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => '品牌',
			'model' => '型号',
                        'money'=>'价格',
			'ctime' => '添加时间',
                        'description'=>'简介',
                        'thumbs'=>'展示图片',
                        'configuration'=>'标准配置',
			'mtime' => '更新时间',
                        'color'=>'颜色',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('model',$this->model,true);
		$criteria->compare('ctime',$this->ctime,true);
		$criteria->compare('mtime',$this->mtime,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}